package com.mk.cloud.pt.controller.platform;

import act.apidoc.Description;
import act.controller.Controller;
import act.inject.DefaultValue;
import com.mk.cloud.common.domain.GlArea;
import com.mk.cloud.common.domain.GlAreaAgent;
import com.mk.cloud.common.domain.PTeamArea;
import com.mk.cloud.common.domain.PTeamAreaArea;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import com.mk.cloud.common.interceptor.header.IHLogin;
import com.mk.cloud.common.util.ResUtils;
import org.osgl.util.S;import com.mk.cloud.common.util.S2;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.http.H;
import org.osgl.mvc.annotation.DeleteAction;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.With;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("teamArea")
public class PTeamAreaController extends CookieService{

    @Inject
    private PTeamArea.Mapper pTeamAreaMapper;
    @Inject
    private PTeamAreaArea.Mapper areaAreaMapper;
    @Inject
    private GlAreaAgent.Mapper glAreaAgentMapper;

    @Description("分组区域--分组区域列表")
    @With(ICLogin.class)
    @GetAction("pageList.json")
    public PageQuery<PTeamArea> pTeamAreaList(
            @Description("筛选条件-分组区域") String taname,
            @Description("当前第几页") @NotNull Integer currentPage,
            @Description("每页多少条") @DefaultValue("15") Integer pageSize

    ) {
        PageQuery<PTeamArea> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String, Object> map = new HashMap<>();
        map.put("taarea", taname);
        map.put("coid", me.getCoid());
        query.setParas(map);
        //此处的sql是三个sql
        pTeamAreaMapper.getSQLManager().pageQuery("pTeamArea.select_team_area_page_list", PTeamArea.class, query);
        return query;
    }

    @Description("该区域分组已选中区域")
    @With(ICLogin.class)
    @GetAction("checkedarea.json")
    public List<GlArea> checkedarea(
            @Description("分组区域") String taid

    ) {
        return pTeamAreaMapper.getCheckedAreas(taid);
    }

    @Description("该企业 已选中区域")
    @With(ICLogin.class)
    @GetAction("checkedareacoid.json")
    public List<PTeamAreaArea> getCheckedAreasByCoid(
    ) {
        return pTeamAreaMapper.getCheckedAreasByCoid(me.getCoid());
    }

    @Description("保存分组区域")
    @With({ICLogin.class, ITransacte.class})
    @PostAction("save.json")
    public String savePTeamArea(
            @Description("保存分组区域") PTeamArea teamArea
    ) {
        teamArea.setCoid(me.getCoid());
        if(S.isNotBlank(teamArea.getTaid())){
            pTeamAreaMapper.updateTemplateById(teamArea);
            return teamArea.getTaid();
        }else{
            teamArea.setTaid(S2.UUID());
            pTeamAreaMapper.insertTemplate(teamArea);
            return teamArea.getTaid();
        }
    }

    @Description("删除分组区域")
    @With(ICLogin.class)
    @DeleteAction("{taid}/del.json")
    public Integer del(
            @Description("主键") String taid
    ) {
        return pTeamAreaMapper.deleteById(taid);
    }

    @Description("保存分组区域---对应区域")
    @With({ICLogin.class, ITransacte.class})
    @PostAction("saveCheckedArea.json")
    public Integer saveCheckedArea(
            @Description("保存分组区域") String taid,
            @Description("对应区域") List<GlArea> areas
    ) {
        pTeamAreaMapper.deleteAreaAreaByTaid(taid);
        areas.forEach(area->{
            PTeamAreaArea areaArea = new PTeamAreaArea();
            areaArea.setTaid(taid);
            areaArea.setAreacode(area.getAreacode());
            areaArea.setCoid(me.getCoid());
            areaAreaMapper.insert(areaArea);
        });
        return areas.size();
    }

    @Description("分组区域")
    @GetAction("teamareas.json")
    @With(ICLogin.class)
    public List<PTeamArea> getPTeamAreaList(){
        if(S.isNotBlank(me.getCoid())){
            PTeamArea teamArea = new PTeamArea();
            teamArea.setCoid(me.getCoid());
            return pTeamAreaMapper.template(teamArea);
        }else{
            return pTeamAreaMapper.all();
        }
    }

    @Description("代理用户选中的的品牌区域")
    @GetAction("agentteamareas.json")
    @With(ICLogin.class)
    public List<String> getAgentPTeamAreaList(String userid){
        return glAreaAgentMapper.getSelectedTaids(userid);
    }

    @Description("代理用户选中的的品牌区域")
    @PostAction("saveagentareas.json")
    @With({ICLogin.class,ITransacte.class})
    public String saveAgentPTeamAreaList(List<PTeamAreaArea> teamAreaAreas, String userid){
        glAreaAgentMapper.saveTeamAreaUserid(teamAreaAreas,userid);
        return "0";
    }





}
